package com.wuzhen.wzzt.service.impl;

import com.wuzhen.wzzt.model.ActiveInfo;
import com.wuzhen.wzzt.model.EnrollUser;
import com.wuzhen.wzzt.vo.TemplateData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
@Slf4j
public class ActivityMessagePushService {
    private final ActiveServiceImpl activeService;
    private final EnrollServiceImpl enrollService;
    private final WxPushService wxPushService;

    public void test() {

        List<ActiveInfo> activeInfoList = activeService.selectActByActiveStartTime();

        if (!CollectionUtils.isEmpty(activeInfoList)) {

            for (ActiveInfo activeInfo : activeInfoList) {
                String id = activeInfo.getId();
                List<EnrollUser> enrollUserList = enrollService.selectEnrollUserByActiveNo(id);
                if (!CollectionUtils.isEmpty(enrollUserList)) {
                    for (EnrollUser user : enrollUserList) {
                        Map<String, TemplateData> m = new HashMap<>();
                        m.put("name1", new TemplateData(filter(user.getNickName())));
                        m.put("thing2", new TemplateData(filter(activeInfo.getActiveDesc())));
                        m.put("time3", new TemplateData(filter(activeInfo.getActiveStartDate())));
                        m.put("thing4", new TemplateData(filter(activeInfo.getAddress())));
                        m.put("thing5", new TemplateData("请您提前安排好时间"));
                        wxPushService.push(user.getOpenId(),m);

                    }
                }


            }
        }

    }

    private String filter(String str){
        if(StringUtils.isEmpty(str)){
            return null;
        }
        if(str.length() < 10){
            return str;
        }
        return str.substring(0,10);

    }

}
